/*
 * Copyright (c) 2022 TumorIsKnowledge Device Co., Ltd.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import storage from '@system.storage';
import prompt from '@system.prompt'
import router from '@system.router'
import http from '@ohos.net.http'
export default {
    data: {
        phone: '',
        psw: '',
        name: '',
        idNumber: '',
        date: '1990-01-01',
        gender: 'male',
        area: '亚洲',
    },

    getPhone(e) {
        this.phone = e.value;
        console.info("phone=" + this.phone)
    },

    getPsw(e){
        this.psw = e.value;
    },

    getName(e){
        this.name = e.value;
    },

    getIdNumber(e){
        this.idNumber = e.value;
    },

    getDate(e) {
        this.date = e.year + '-' + (e.month + 1) + '-' + e.day;
        console.info("date=" + this.date)
    },

    getFemaleGender(e) {
        if (e.checked) {
            this.gender = 'female'
        }
    },

    getMaleGender(e) {
        if (e.checked) {
            this.gender = 'male'
        }
    },

    getArea(e) {
        this.area = e.newValue;
        console.info("area=" + this.area)
    },

    onRegister() {
        const reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
        if(!reg.test(this.phone)){
            prompt.showToast({  // 提示请求错误
                message: '手机号非法',
                duration: 3000,
            })
            return;
        }

        if (this.psw.length < 6) {
            prompt.showToast({
                message: this.$t('密码至少为6位')
            })
            return;
        }

        if (this.name.length == 0) {
            prompt.showToast({
                message: this.$t('姓名不能为空')
            })
            return;
        }

        if (this.date.length == 0) {
            prompt.showToast({
                message: this.$t('日期不能为空')
            })
            return;
        }

        let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        let _IDRe15 =  /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/

        if(!_IDRe18.test(this.idNumber) && !_IDRe15.test(this.idNumber)){
            prompt.showToast({  // 提示请求错误
                message: '身份证号非法'+this.idNumber,
                duration: 3000,
            })
            return;
        }
        this.$element('hintDialog').show()
    },

    sethintDialog(e) {
        this.$element('hintDialog').close()
        // let声明的变量的作用域是块或者子块
        let httpRequest = http.createHttp();
        // 创建一个http，里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header 事件。
        // 每一个HttpRequest对象对应一个Http请求。如需发起多个Http请求，须为每个Http请求创建对应HttpRequest对象。
        //返回一个HttpRequest对象，里面包括request、destroy、on和off方法。

        let url = "http://124.70.109.103:8000/userRegister/";

        httpRequest.request(url, {
            // 注意请求方法：http.POST
            method: 'POST',
            extraData: {
                phone: this.phone,
                password: this.psw,
                name: this.name,
                gender: this.gender,
                date: this.date,
                idNumber: this.idNumber,
            },
        }, (err, data)=> {  // 判断是否请求成功
            if (!err) {  // 请求成功
                this.returnContent = JSON.parse(data.result);  //JSON.parse(字符串)——将字符串转换成json数据格式
                var code = this.returnContent.code;  //使用json将province赋值给code
                if(code==400){
                    prompt.showToast({  // 提示请求错误
                        message: this.returnContent.errmsg,
                        duration: 3000,
                    })
                }

                if(code==200){

                    storage.set({
                        key: 'id',
                        value: this.phone,
                    });

                    storage.set({
                        key: 'psw',
                        value: this.psw,
                    });

                    router.push ({
                        uri:'pages/index/index',  //跳转到指定页面
                    })
                    prompt.showToast({  // 提示信息
                        message: this.returnContent.message,
                        duration: 3000,
                    })
                }

            } else {  // 请求失败
                prompt.showToast({
                    message: data.result,
                    duration: 3000,
                });
            }
        })
    },

    last() {
        router.push({
            uri:'pages/login/login', // 指定要跳转的页面
        })
    },


    //    // 生日选择器
    //    chooseBirthday(e) {
    //        let month = (e.month + 1) + "";
    //        if (month.length == 1) {
    //            month = "0" + month;
    //        }
    //        let day = e.day + "";
    //        if (day.length == 1) {
    //  tex          day = "0" + day;
    //        }
    //        let birthday = e.year + "-" + month + "-" + day;
    //        this.birthday = birthday;
    //        this.birthdayVal = birthday;
    //    },

}
